import React, { useEffect, useState } from "react";
import axios from "axios";
//hooks函数的规则【就是和普通函数的区别】
// 1、以use开头
// 2、可以使用组件相关的特性（如：状态，生命周期）
// 3、hooks的调用：只能在函数式组件里（根级调用）和其它hooks里。

export function useCount() {
  const [count, setCount] = React.useState(3);

  function add(inc = 1) {
    setCount(count + inc);
  }

  function sub() {
    setCount(count + 1);
  }

  return [count, add, sub];
}

export function useBooks(params = {}) {
  const [books, setBooks] = React.useState([]);

  useEffect(() => {
    axios({
      url: "http://localhost:3000/books",
      params
    }).then(res => {
      if (res.data) {
        setBooks(res.data);
      }
    })
  }, [])

  return books;
}

export function useProList(page=1,pageSize=10) {
  const [proList, setProList] = useState([]);

  useEffect(() => {
    console.log("获取商品列表");
    axios({
      url: "http://121.89.205.189:3000/api/pro/list",
      params:{
        count:page,
        limitNum:pageSize
      }
    }).then(res => {
      if(res.data.code==="200"){
        setProList(res.data.data);
      }
    })
  }, [page,pageSize])

  return proList

}

export function useWindowResize() {
  
  window.onresize = () => {
    console.log("窗口大小改变");
  }

  window.onmousemove = () => {
    console.log("鼠标移动");
  }
}